package xin.sunzy.spring.controller.listener;

import com.google.common.eventbus.EventBus;
import com.google.common.eventbus.Subscribe;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import xin.sunzy.spring.constant.Constants;
import xin.sunzy.spring.event.LoginEvent;

import javax.annotation.PostConstruct;
import javax.servlet.http.HttpServletRequest;

/**
 * @description: 用户登录监听器
 * @email: <a href="mailto:sunzhenyu@terminus.io">孙振宇</a>
 * @author: ginkgo
 * @date: 2018/4/4
 */
@Component
public class UserLoginListener {

    @Autowired
    private EventBus eventBus;

    @PostConstruct
    public void init() {
        this.eventBus.register(this);
    }

    @Subscribe
    public void dispatch(LoginEvent event) {
        HttpServletRequest request = event.getRequest();
        request.getSession()
                .setAttribute(Constants.SESSION_USER_ID, event.getUser().getId());
    }
}
